/**
 * 
 */
package com.ddyx.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.servlet.http.HttpServletRequest;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import com.ddyx.entity.EventAttenStore;
import com.ddyx.mapper.EventAttenStoreMapper;
import com.ddyx.service.IEventAttenStoreService;
import com.ddyx.util.EventTag;
import com.ddyx.util.MD5FileUtil;
import com.ddyx.util.MQUtil;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: EventAttenStoreServiceImpl.java
 * @Description: 
 * @author: 
 * @date: 2017年12月27日
 */

@Slf4j
@Service
public class EventAttenStoreServiceImpl implements IEventAttenStoreService {
	@Resource
	EventAttenStoreMapper eventAttenStoreMapper;
	@Resource
	JmsTemplate jmsTemplate;
	@Resource
	Destination topicDestination;
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm insertAttenStore(RequestForm param,HttpServletRequest req) {
		ResponseForm result = new ResponseForm();
		if (param!=null) {
			try {
				Date date = new Date();
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				EventAttenStore bean = new EventAttenStore();
				MyBeanUtils.populate(bean, paramMap);
				bean.setEventTime(date);
				eventAttenStoreMapper.insertSelective(bean);
				
				//mq同步事件日志
				String requestUrl = req.getRequestURL().toString();
				paramMap.put("requestUrl",requestUrl);
				paramMap.put("urlMd5",MD5FileUtil.getMD5String(requestUrl));
				paramMap.put("actionType", EventTag.ATTENTIONSTORE);
				paramMap.put("eventTime", date);
				MQUtil.sendObMsg(paramMap, jmsTemplate, topicDestination);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm cancelAttenStore(RequestForm param, HttpServletRequest req) {
		ResponseForm result = new ResponseForm();
		if (param!=null) {
			try {
				Date date = new Date();
				Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				EventAttenStore bean = new EventAttenStore();
				MyBeanUtils.populate(bean, paramMap);
				eventAttenStoreMapper.delete(bean);
				
				//mq同步事件日志
				String requestUrl = req.getRequestURL().toString();
				paramMap.put("requestUrl",requestUrl);
				paramMap.put("urlMd5",MD5FileUtil.getMD5String(requestUrl));
				paramMap.put("eventTime", date);
				paramMap.put("actionType", EventTag.CANCELATTENTIONSTORE);
				MQUtil.sendObMsg(paramMap, jmsTemplate, topicDestination);
				
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}

	/* 
	 * 获取用户关注过 的门店信息
	 */
	@Override
	public ResponseForm attenAllStore(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			try {
				paramMap = com.ddyx.util.Page.parsePage(paramMap);
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				List list = eventAttenStoreMapper.attenAllStore(paramMap);
				result.setTotal((int) pageHelper.getTotal());
				result.setData(list);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}
	
	
}
